package com.itheima.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.collection.CollectionUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.CharUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.http.MediaType;


@Slf4j
public class ExcelUtils {

    public static void writeExcel(HttpServletResponse response, ReportExcelDto reportExcelDto, List<Map<String, String>> dataList) {
        Workbook wb = new XSSFWorkbook();
        Sheet sheet = wb.createSheet("Sheet1");
        sheet.setDefaultColumnWidth(16);

        Row row1 = sheet.createRow(0);
        Cell row1Cell1 = row1.createCell(0);
        row1Cell1.setCellValue("会员数据统计");
        row1Cell1.getCellStyle().setAlignment(CellStyle.ALIGN_CENTER);

        Row row2 = sheet.createRow(1);
        Cell row2Cell1 = row2.createCell(0);
        row2Cell1.setCellValue("今日新增会员数");
        Cell row2Cell2 = row2.createCell(1);
        row2Cell2.setCellValue(reportExcelDto.getTodayNewMember());
        Cell row2Cell3 = row2.createCell(2);
        row2Cell3.setCellValue("总会员数");
        Cell row2Cell4 = row2.createCell(3);
        row2Cell4.setCellValue(reportExcelDto.getTotalMember());

        Row row3 = sheet.createRow(2);
        Cell row3Cell1 = row3.createCell(0);
        row3Cell1.setCellValue("本周新增会员数");
        Cell row3Cell2 = row3.createCell(1);
        row3Cell2.setCellValue(reportExcelDto.getThisWeekNewMember());
        Cell row3Cell3 = row3.createCell(2);
        row3Cell3.setCellValue("本月新增会员数");
        Cell row3Cell4 = row3.createCell(3);
        row3Cell4.setCellValue(reportExcelDto.getThisMonthNewMember());

        Row row4 = sheet.createRow(3);
        Cell row4Cell1 = row4.createCell(0);
        row4Cell1.setCellValue("预约到诊数据统计");
        row4Cell1.getCellStyle().setAlignment(CellStyle.ALIGN_CENTER);

        Row row5 = sheet.createRow(4);
        Cell row5Cell1 = row5.createCell(0);
        row5Cell1.setCellValue("今日预约数");
        Cell row5Cell2 = row5.createCell(1);
        row5Cell2.setCellValue(reportExcelDto.getTodayOrderNumber());
        Cell row5Cell3 = row5.createCell(2);
        row5Cell3.setCellValue("今日到诊数");
        Cell row5Cell4 = row5.createCell(3);
        row5Cell4.setCellValue(reportExcelDto.getTodayVisitsNumber());

        Row row6 = sheet.createRow(5);
        Cell row6Cell1 = row6.createCell(0);
        row6Cell1.setCellValue("本周预约数");
        Cell row6Cell2 = row6.createCell(1);
        row6Cell2.setCellValue(reportExcelDto.getThisWeekOrderNumber());
        Cell row6Cell3 = row6.createCell(2);
        row6Cell3.setCellValue("本周到诊数");
        Cell row6Cell4 = row6.createCell(3);
        row6Cell4.setCellValue(reportExcelDto.getThisWeekVisitsNumber());

        Row row7 = sheet.createRow(6);
        Cell row7Cell1 = row7.createCell(0);
        row7Cell1.setCellValue("本月预约数");
        Cell row7Cell2 = row7.createCell(1);
        row7Cell2.setCellValue(reportExcelDto.getThisMonthOrderNumber());
        Cell row7Cell3 = row7.createCell(2);
        row7Cell3.setCellValue("本月到诊数");
        Cell row7Cell4 = row7.createCell(3);
        row7Cell4.setCellValue(reportExcelDto.getThisMonthVisitsNumber());

        Row row8 = sheet.createRow(7);
        Cell row8Cell1 = row8.createCell(0);
        row8Cell1.setCellValue("热门套餐");
        row8Cell1.getCellStyle().setAlignment(CellStyle.ALIGN_CENTER);

        Row row9 = sheet.createRow(8);
        Cell row9Cell1 = row9.createCell(0);
        row9Cell1.setCellValue("套餐名称");
        Cell row9Cell2 = row9.createCell(1);
        row9Cell2.setCellValue("预约数量");
        Cell row9Cell3 = row9.createCell(2);
        row9Cell3.setCellValue("占比");
        Cell row9Cell4 = row9.createCell(3);
        row9Cell4.setCellValue("备注");

        if (CollectionUtil.isNotEmpty(dataList)) {
            for (int i = 0; i < dataList.size(); i++) {
                Row sheetRow = sheet.createRow(i + 9);
                Cell sheetRowCell1 = sheetRow.createCell(0);
                sheetRowCell1.setCellValue(String.valueOf(dataList.get(i).get("name")));
                Cell sheetRowCell2 = sheetRow.createCell(1);
                sheetRowCell2.setCellValue(String.valueOf(dataList.get(i).get("setmeal_count")));
                Cell sheetRowCell3 = sheetRow.createCell(2);
                sheetRowCell3.setCellValue(String.valueOf(dataList.get(i).get("proportion")));
                Cell sheetRowCell4 = sheetRow.createCell(3);
                sheetRowCell4.setCellValue(String.valueOf(dataList.get(i).get("remark")));
            }
        }

        //冻结窗格
        wb.getSheet("Sheet1").createFreezePane(0, 1, 0, 1);
        //浏览器下载excel
        buildExcelDocument("运营报表统计.xlsx", wb, response);
    }

    /**
     * 浏览器下载excel
     *
     * @param fileName
     * @param wb
     * @param response
     */
    private static void buildExcelDocument(String fileName, Workbook wb, HttpServletResponse response) {
        try {
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8"));
            response.flushBuffer();
            wb.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
